package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.PipeBaseQuery;
import com.deyuanyun.pic.pipe.controller.vo.ReducerVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “异管径”控制类
 * @author xiatao
 * 时间:2015年08月25日 11时19分52秒
 */
@Controller
@RequestMapping("/pipe/reducer")
public class ReducerController extends PipeBaseController {

    @Resource
    private PipeMgrService pipeMgrService;

    /**
     * 跳转到 异管径 主页面
     *
     * @return
     */
    @RequestMapping("/toReducer")
    public ModelAndView toReducer(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("basedata/pipe/reducer");
        return mv;
    }

    /**
     * 根据条件分页查询 异管径 数组对象
     *
     * @param pipeBaseQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(PipeBaseQuery pipeBaseQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(pipeBaseQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据条件导出 异管径
     *
     * @param pipeBaseQuery
     * @throws IOException
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(PipeBaseQuery pipeBaseQuery) throws IOException {
        List list = this.queryVO(pipeBaseQuery);
        PipeExportEnum exportEnum = PipeExportEnum.Reducer;
        String[] columnKeys = {"pipeLineName", "reducerNumber", "mileage", "reference", "referenceDistance",
                "manufacturedDate", "reducerType", "material", "pressureRating", "outletConnectionType",
                "outletDiameter","outletwallthickness", "recorder", "recordTime", "remark"};
        DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private List queryVO(PipeBaseQuery pipeBaseQuery) {
        return new ReducerVO().fromDomainList(pipeMgrService.queryReducer(pipeBaseQuery));
    }
}
